package com.itheima.sc.impl.pms;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.sc.api.pms.ProductApi;
import com.itheima.sc.config.ComException;
import com.itheima.sc.mapper.pms.ProductMapper;
import com.itheima.sc.pojo.pms.Brand;
import com.itheima.sc.pojo.pms.Product;
import org.apache.dubbo.config.annotation.DubboService;

import java.util.List;

@DubboService
public class ProductImpl extends ServiceImpl<ProductMapper, Product> implements ProductApi {
    @Override     //查询商品
    public List<Product> listAll(Integer brandId, String keyword, Integer pageNum, Integer pageSize, Integer productCategoryId, String productSn, Integer publishStatus, Integer verifyStatus) {
        LambdaQueryWrapper<Product> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(StrUtil.isNotBlank(keyword),Product::getName,keyword);
        wrapper.eq(ObjectUtil.isNotEmpty(brandId),Product::getBrandId, Convert.toLong(brandId));
        wrapper.eq(ObjectUtil.isNotEmpty(productCategoryId),Product::getProductCategoryId,Convert.toLong(productCategoryId));
        wrapper.eq(StrUtil.isNotBlank(productSn),Product::getProductSn,productSn);
        wrapper.eq(ObjectUtil.isNotEmpty(publishStatus),Product::getPublishStatus,publishStatus);
        wrapper.eq(ObjectUtil.isNotEmpty(verifyStatus),Product::getVerifyStatus,verifyStatus);

        Page<Product> pageInfo = new Page<>(pageNum, pageSize);
        pageInfo = page(pageInfo, wrapper);
        List<Product> productList = pageInfo.getRecords();

        return productList;
    }

    @Override       //条件查询totalCount
    public Integer getCount(Integer brandId, String keyword, Integer pageNum, Integer pageSize, Integer productCategoryId, String productSn, Integer publishStatus, Integer verifyStatus) {
        LambdaQueryWrapper<Product> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(StrUtil.isNotBlank(keyword),Product::getName,keyword);
        wrapper.eq(ObjectUtil.isNotEmpty(brandId),Product::getBrandId, Convert.toLong(brandId));
        wrapper.eq(ObjectUtil.isNotEmpty(productCategoryId),Product::getProductCategoryId,Convert.toLong(productCategoryId));
        wrapper.eq(StrUtil.isNotBlank(productSn),Product::getProductSn,productSn);
        wrapper.eq(ObjectUtil.isNotEmpty(publishStatus),Product::getPublishStatus,publishStatus);
        wrapper.eq(ObjectUtil.isNotEmpty(verifyStatus),Product::getVerifyStatus,verifyStatus);

        int count = count(wrapper);
        return count;
    }

    @Override      //根据商品名称或货号模糊查询
    public List<Product> simpleList(String keyword) {
        LambdaQueryWrapper<Product> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(StrUtil.isNotBlank(keyword),Product::getName,keyword);

        List<Product> productList = list(wrapper);
        return productList;
    }

    @Override      //批量修改删除状态
    public void updateDeleteStatus(Integer deleteStatus, List<Integer> ids) {
        List<Product> productList = listByIds(ids);
        //删除品牌判断,如果已上架,不能删除
        for (Product product : productList) {
            //product.setDeleteStatus(deleteStatus);
            removeById(product.getId());
        }
    }

    @Override     //批量设为新品
    public void updateNewStatus(Integer newStatus, List<Integer> ids) {
        List<Product> productList = listByIds(ids);
        for (Product product : productList) {
            product.setNewStatus(newStatus);
            updateById(product);
        }
    }

    @Override //批量上下架商品
    public void updatePublishStatus(Integer publishStatus, List<Integer> ids) {
        List<Product> productList = listByIds(ids);
        for (Product product : productList) {
            product.setPublishStatus(publishStatus);
            updateById(product);
        }
    }

    @Override //批量推荐商品
    public void updateRecommendStatus(Integer recommendStatus, List<Integer> ids) {
        List<Product> productList = listByIds(ids);
        for (Product product : productList) {
            product.setRecommandStatus(recommendStatus);
            updateById(product);
        }
    }

    @Override
    public Integer savePro(Product product) {
        save(product);
        Integer pid = Convert.toInt(product.getId());
        return pid;
    }


}
